﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FtpWorkerRole.FtpServer;

namespace FtpWorkerRole.FtpCommand
{
    class DeleCommand : CommandBase
    {
        public DeleCommand(FtpSocketHandler handler)
            : base(handler)
        {
            commandName = "DELE";
        }

        public override void execute(string value)
        {
            if (!handler.Login)
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(530, "Please Login!"));
                return;
            }
            
            value = value.Trim().Replace(@"/", @"\");
            var path = Path.Combine(handler.currentDirectory, value);

            if (handler.fileSystem.delete(path))
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(250, "File deleted successfully"));
            }
            else
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(550, "File does not exist!"));
            }
        }
    }
}
